package com.ssm.security.token;

import com.ssm.security.JWT.JSONResult;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 *
 */
public class serviceUnauthorizedEntryPoint implements AuthenticationEntryPoint {

    /**
     * 匿名(检测不到用户信息，有可能是token获取不到用户信息)情况下权限不足访问时报错
     *
     * @param request
     * @param response
     * @param arg2
     * @throws IOException
     * @throws ServletException
     */
    @Override
    public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException arg2) throws IOException, ServletException {
        try {
            if(!response.isCommitted()){
                response.setContentType("application/json");
                response.setStatus(HttpServletResponse.SC_OK);
                response.setHeader("Authentication",JSONResult.fillResultString(700, arg2.getMessage(), null, null,null));
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
